﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.UI;
using Warensoft.DataService.Models;
using Warensoft.Unity.Communication.Client.DbClient;

namespace Warensoft.DataService
{
    public class SqlDataService
    {
        private InstantiableDBHelper dbHelper;
        private static SqlDataService innerService;
        private string connectionStringName;
        /// <summary>
        /// 获取连接字符串
        /// </summary>
        public string ConnectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings[this.connectionStringName].ConnectionString;
            }
        }
        /// <summary>
        /// 设置或获取宿主页面
        /// </summary>
        public Page HostPage { get; set; }



        public SqlDataService(Page hostPage, string connectionStringName)
        {
            this.HostPage = hostPage;
            this.connectionStringName = connectionStringName;
            this.dbHelper = new InstantiableDBHelper(this.ConnectionString);
            this.ProcessReqeust();
        }
        private void ProcessReqeust()
        {
            if (this.HostPage.Request.ContentLength != 0 && this.HostPage.Request.HttpMethod == "POST")
            {
                var strm = this.HostPage.Request.InputStream;
                var buffer = new byte[this.HostPage.Request.ContentLength];
                strm.Read(buffer, 0, buffer.Length);


                if (this.HostPage.Request["Command"] != null)
                {

                    Query query= new Query(buffer);
                    ResultInfo result = null;
                    switch (this.HostPage.Request["Command"])
                    {
                        case "CreateDataSet":
                            result = this.dbHelper.CreateDataSet(query);
                            break;
                        case "ExecuteScalar":
                            result = this.dbHelper.ExecuteScalar(query);
                            break;
                        case "ExecuteNonQuery":
                            result = this.dbHelper.ExecuteNonQuery(query);
                            break;
                        case "CreateDataSetSchema":
                            result=this.dbHelper.CreateDataSetSchema(query.CommandText);

                            break;
                        case "UpdateDataSet":
                            result = this.dbHelper.UpdateDataSet(query .DataSet,query .CommandText );
                            break;
                        default:
                            break;
                    }
                    if (result !=null )
                    {
                        this.HostPage.Response.BinaryWrite(result .ToArray ());
                    }
                }
                this.HostPage.Response.End();
            }
        }
    }
}
